package com.education.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.education.common.component.TenantProperties;
import com.education.common.handler.CustomTenantHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zengjintao
 * @version 1.0
 * @create_at 2020/11/15 13:23
 */
@Slf4j
@Configuration
public class MybatisPlusConfig {

    @Autowired
    private TenantProperties tenantProperties;

    private final CustomTenantHandler tenantHandler;


    public MybatisPlusConfig(CustomTenantHandler tenantHandler) {
        this.tenantHandler = tenantHandler;
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        PaginationInnerInterceptor innerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        if (Boolean.TRUE.equals(tenantProperties.isEnabled())) {
            // 启用多租户插件拦截
            log.info("启用多租户插件拦截启用");
            mybatisPlusInterceptor.addInnerInterceptor(new TenantLineInnerInterceptor(tenantHandler));
        }

        mybatisPlusInterceptor.addInnerInterceptor(innerInterceptor);
        return mybatisPlusInterceptor;
    }
}
